//
//  HHPuckerList2ViewController.m
//  Flower
//
//  Created by 花花 on 2016/12/10.
//  Copyright © 2016年 花花. All rights reserved.
//

#import "HHPuckerList2ViewController.h"
#import "Pucker2Cell.h"
#import "PuckerModel.h"
#import <objc/runtime.h>
#define kCell_Height 44
@interface HHPuckerList2ViewController ()<UITableViewDelegate,UITableViewDataSource>{
UITableView *TableView;
NSMutableArray *Datasource;
}

@end

static NSString *const pucker2ID = @"pucker2ID";
static char* const buttonKey = "buttonKey";

@implementation HHPuckerList2ViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupTableView];
    [self loadData];
    
}
- (void)loadData{
    Datasource = [[NSMutableArray alloc]init];
    
    
    //    [jsonDic writeToFile:@"/Users/huahua/Desktop/pucker.plist" atomically:YES];
    NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"content.plist" ofType:nil];
    NSMutableDictionary *data = [[NSMutableDictionary alloc] initWithContentsOfFile:plistPath];
    NSArray *ArrData =data[@"group"];
    for (NSDictionary *dic in ArrData) {
        
        PuckerModel *model = [[PuckerModel alloc]init];
        
        model.groupName = dic[@"groupName"];
        model.isOpened = NO;
        model.groupCount = [dic[@"groupCount"] integerValue];
        model.groupFriends = dic[@"groupArray"];
        [Datasource addObject:model];
    }
}

#pragma mark - 设置TableView
- (void)setupTableView{
    
    TableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
    TableView.delegate  = self;
    TableView.dataSource = self;
    TableView.rowHeight = 80;
    TableView.tableFooterView = [UIView new];
    [TableView registerNib:[UINib nibWithNibName:(NSStringFromClass([Pucker2Cell class])) bundle:nil] forCellReuseIdentifier:pucker2ID];
    [self.view addSubview:TableView];
    
}
#pragma mark - <UITableViewDelegate,UITableViewDataSource>
//TableView一共有多少分区
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return Datasource.count;
}
//每个分区有多少行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    PuckerModel *model = Datasource[section];
    NSInteger count = model.isOpened?model.groupFriends.count:0;
    return count;
}
//绘制每一行
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    Pucker2Cell  *cell = [tableView dequeueReusableCellWithIdentifier:pucker2ID];
    PuckerModel *model = Datasource[indexPath.section];
    NSDictionary *dic = model.groupFriends[indexPath.row];
    cell.name.text = dic[@"name"];
    
    if ([dic[@"status"] isEqualToString:@"1"]) {
        cell.is_on_line.textColor = [UIColor redColor];
        cell.is_on_line.text = @"在线";
    }else{
        cell.is_on_line.textColor = [UIColor lightGrayColor];
        cell.is_on_line.text = @"不在线";
    }
    cell.shuoshuo.text = dic[@"shuoshuo"];
    cell.downbtn.hidden = YES;
    cell.backgroundColor = [UIColor whiteColor];
    cell.selectionStyle = UITableViewCellSelectionStyleDefault;
    
    return cell;
}

//自定义section的显示内容，自定义的话 则上面既没有意义了
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    PuckerModel*model = Datasource[section];
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setFrame:CGRectMake(0, 0, self.view.frame.size.width, 44)];
    [button setTag:section+1];
    [button addTarget:self action:@selector(sectionbuttonClick:) forControlEvents:UIControlEventTouchUpInside];
    
    UIImageView *line = [[UIImageView alloc]initWithFrame:CGRectMake(0, button.frame.size.height-1, button.frame.size.width, 1)];
    line.image =[UIImage imageNamed:@"line_real"];
    [button addSubview:line];
    
    if (model.isOpened) {
        
        UIImageView *imgview = [[UIImageView alloc]initWithFrame:CGRectMake(10, (44-16)/2, 14, 16)];
        imgview.image =[UIImage imageNamed:@"ico_list"];
        [button addSubview:imgview];
        CGAffineTransform currentTransform = imgview.transform;
        CGAffineTransform newTransForm = CGAffineTransformRotate(currentTransform, M_PI/2);
        imgview.transform= newTransForm;
        objc_setAssociatedObject(button, buttonKey, imgview, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
        
    }else{
        
        UIImageView *imgView = [[UIImageView alloc]initWithFrame:CGRectMake(10, (44-16)/2, 14, 16)];
        imgView.image = [UIImage imageNamed:@"ico_list"];
        [button addSubview:imgView];
        objc_setAssociatedObject(button, buttonKey, imgView, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    }
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(30,(44-22)/2 , self.view.frame.size.width-60-26, 22)];
    label.backgroundColor = [UIColor greenColor];
    label.font = [UIFont systemFontOfSize:15];
    label.text  = model.groupName;
    [button addSubview:label];
    
    
    UILabel *numberLabel = [[UILabel alloc]initWithFrame:CGRectMake(self.view.frame.size.width-50, (44-22)/2,40 , 22)];
    NSInteger on_lineCount = 0;
    for (NSDictionary *dic in model.groupFriends) {
        
        if ([dic[@"status"] isEqualToString:@"1"]) {
            on_lineCount++;
            
        }}
    [numberLabel  setText:[NSString stringWithFormat:@"%ld/%ld",(long)(long)on_lineCount,(long)model.groupCount]];
    [button addSubview:numberLabel];
    
    return button;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    PuckerModel *model = Datasource[indexPath.section];
    
    NSDictionary *dic = model.groupFriends[indexPath.row];
    
    NSLog(@"%@--%@",dic[@"name"],dic[@"shuoshuo"]);
    
}
//返回section的高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return kCell_Height;
}

- (void)sectionbuttonClick:(UIButton *)sender{
    PuckerModel *model = Datasource[sender.tag-1];
    UIImageView *imgView= objc_getAssociatedObject(sender, buttonKey);
    if (model.isOpened) {
        [UIView animateWithDuration:0.3 delay:0.3 options:UIViewAnimationOptionLayoutSubviews animations:^{
            CGAffineTransform currentTransform = imgView.transform;
            CGAffineTransform newTransform = CGAffineTransformRotate(currentTransform, -M_PI/2);
            imgView.transform = newTransform;
            
        } completion:^(BOOL finished) {
            
        }];
    }else{
        
        [UIView animateWithDuration:0.3 delay:0.3 options:UIViewAnimationOptionLayoutSubviews animations:^{
            CGAffineTransform currentTransform = imgView.transform;
            CGAffineTransform newTransform = CGAffineTransformRotate(currentTransform, M_PI/2);
            
            imgView.transform = newTransform;
        } completion:^(BOOL finished) {
            
        }];
    }
    model.isOpened =!model.isOpened;
    [TableView reloadSections:[NSIndexSet indexSetWithIndex:sender.tag-1] withRowAnimation:UITableViewRowAnimationAutomatic];
    
}

@end
